home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Development / REALbasic 5.5.5.dmg / REALbasic 5.5.5 Mac OS X / Read Me / About the New REAL Database.txt < prev    next >
Encoding:
Text File  |  2005-03-18  |  14.0 KB  |  325 lines

  1. New REAL Database Engine
  2.  
  3. The database engine that has always shipped with REALbasic is being replaced with a new database engine, also known as the REAL Database (the old engine will hereafter be known as "REAL Database (Old Format)".  This document describes the features and usage of the new REAL database engine.
  4.  
  5.  
  6. General Features
  7.  
  8. A new REAL database can store its data on disk in either of two ways.  It can keep them all neatly encapsulated inside a virtual volume file (a single file on disk which can be treated like a virtual disk in REALbasic), or it can store them as real files in a normal folder on disk.
  9.  
  10. New REAL data tables may be added, dropped, or mutated in various ways (columns or indexes added or dropped), without losing or recopying the existing data.
  11.  
  12. The new REAL database supports a subset of SQL/92 and SQL/99 (details below), including queries that involve self-joins, aggregate functions, and more.  Our intent is that for the set of features the new REAL Database engine supports, its syntax is fully SQL compliant (with a few minor extensions, like the Boolean data type).  It also returns standard SQL error codes.
  13.  
  14. Every new REAL database table has a special column called "_rowid" which is a unique identifier for that row.  This is added automatically, and serves as a convenient join field for building relational databases.
  15.  
  16. An SQLSelect returns a dynamic cursor; you can move forwards, backwards, or jump to the beginning or end as much as you like.
  17.  
  18. The new REAL database engine supports transactions, both for schema changes and for data changes.  Per the SQL standard, a transaction is started automatically when you make any change to the database, and ended by COMMIT or ROLLBACK.
  19.  
  20. The new REAL database also supports encodings.  When you insert records into the database, the encoding of the text fields (which is to say, the encoding of the SQL statement that inserts them) is remembered by the database.  When you retrieve this data later, the encoding is restored.  So, unlike most other database engines, there is no need to use DefineEncoding on text data retrieved from a REAL database.
  21.  
  22.  
  23. Backwards Compatibility Notes
  24.  
  25. The new REAL database does not support column constraints; neither NOT NULL nor PRIMARY KEY will work.  However, you can now index any column, whereas the old one did not support indexes except for the primary key.
  26.  
  27. The new database does not support the use of multi-word column names enclosed in square brackets.  This feature may return if there is sufficient demand, but for now, your column names should not include any whitespace.  (You can use the "AS" syntax to name result columns in your query.)
  28.  
  29. The old database engine supported both single-quotes and double-quotes as string delimiters; per the SQL standard, the new engine supports only single-quotes.
  30.  
  31. The new database engine does not support the MacPICT column type.  It does, however, support a Binary (blob) column type.
  32.  
  33. There are many other minor differences, as we have moved to be more in line with the SQL standards.  But most of the differences are new features which will not affect older projects.  Those new features are briefly described above, and detailed below.  In addition, there were many undocumented limitations of the old engine (e.g., ORDER BY did not work for queries involving a join) which do not exist in the new REAL database.
  34.  
  35.  
  36. Data Types Supported
  37.  
  38. New REAL database fields may be of any of the following types:
  39.  
  40.     Type        Numeric Code    Comments
  41.     ----        ------------    --------
  42.     Integer        3                32-bit signed integer
  43.     VarChar        5                text up to 2^31 bytes (see Note 1)
  44.     Double        7                64-bit floating-point number
  45.     Date        8                day, in YYYY-MM-DD format
  46.     Time        9                time, in HH:MM:SS format
  47.     TimeStamp    10                time stamp, in YYYY-MMM-DD HH:MM:SS format
  48.     Boolean        12                boolean value, TRUE or FALSE (see Note 2)
  49.     Binary        14                binary data up to 2^31 bytes
  50.     String        18                text up to 2^31 bytes (see Note 1)
  51.     
  52. Note 1: Text fields in the new REAL database store encoding information as well as the text bytes.
  53.  
  54. Note 2: Boolean is not a SQL data type, and TRUE or FALSE are not SQL keywords.  These are our own extensions to SQL.
  55.  
  56.  
  57. Creating or Opening a Database in the IDE
  58.  
  59. If you want your new REAL database to be part of your project, then you can use the "Add Data Source" command in the File menu of the IDE to either create a new database file, or attach an existing one.  When you make a new one, it is stored next to your project file, and when you ship your built app.  When you add an existing one, the database file must already bee in the same folder as your project file (or built app).  [In the future, we may search subdirectories of that folder as well.]
  60.  
  61.  
  62. Creating or Opening a Database in REALbasic Code
  63.  
  64. You can also choose to create or open a new REAL database at run-time.  You would do this by creating an instance of the REALDatabase class, assigning an appropriate location to its databaseFile property, and then calling either CreateDatabaseFile (to make a new database) or Connect (to open an existing database).  Note that if the databaseFile refers to an already-existing folder, the data files will be stored within that folder; otherwise a virtual volume will be created and the files stored therein.
  65.  
  66.  
  67. SQL Syntax Supported
  68.  
  69. A semi-formal specification of the SQL syntax supported by the new REAL database engine appears below.  It uses the following conventions: a vertical bar ("|") separates items in a list of alternatives; square brackets ("[" and "]") indicate an optional part which may be included or omitted; and curly braces ("{" and "}") indicate a required part which may not be omitted.  Items in angle brackets (e.g. "<select-item-list>") indicate a more complex part which may be detailed in its own specification entry.  Any such item which ends in "-string" is a simple identifier (e.g. "foo").  Examples are presented in the next section.
  70.  
  71. valid-sql-input:
  72.   {        <select-statement>
  73.       |    <insert-statement>
  74.       |    <update-statement>
  75.       |    <delete-statement>
  76.       |    <create-table-statement>
  77.       |    <create-index-statement>
  78.       |    <alter-table-statement>
  79.       |    <drop-table-statement>
  80.       |   <drop-index-statement>
  81.       |    COMMIT
  82.       |    ROLLBACK
  83.   }
  84.  
  85. select-statement:
  86.     SELECT [ ALL | DISTINCT ] <select-item-list>
  87.         FROM <table-list>
  88.         [ WHERE <expression> ]
  89.         [ GROUP BY <column-ref-list> ]
  90.         [ HAVING <expression> ]
  91.  
  92. select-item-list: one or more comma-separated items of the form:
  93.     <column-expression> [ [ AS ] <column-name-string> ]
  94.  
  95. table-list: one or more comma-separated items of the form:
  96.     <table-name-string> [ [ AS ] <range-variable-string> ]
  97.  
  98. insert-statement:
  99.     INSERT INTO <table-name-string> [ ( <column-name-list> ) ]
  100.         {    DEFAULT VALUES
  101.           | VALUES ( <expression-list> )
  102.           | <select-statement>
  103.         }
  104.  
  105. update-statement:
  106.     UPDATE <table-name-string> SET <assignment-list>
  107.         [ WHERE <expression> ]
  108.  
  109. assignment-list: one or more comma-separated items of the form:
  110.     <column-name-string> = { <expression> | DEFAULT | NULL }
  111.  
  112. delete-statement:
  113.     DELETE FROM <table-name-string>
  114.         [ WHERE <expression> ]
  115.  
  116. create-table-statement:
  117.     CREATE TABLE <table-name-string> ( <column-def-list> )
  118.  
  119. column-def-list: one or more comma-separated items of the form:
  120.     <column-name-string> <column-type-string>
  121.  
  122. create-index-statement:
  123.     CREATE INDEX <index-name-string> ON <table-name-string>
  124.         ( <column-name-list )
  125.  
  126. alter-table-statement:
  127.     ALTER TABLE <table-name-string>
  128.         {    ADD [ COLUMN ] <column-name-string> <column-type-string>
  129.           | DROP [ COLUMN ] <column-name-string> { RESTRICT | CASCADE }
  130.         }
  131.  
  132. drop-table-statement:
  133.     DROP TABLE <table-name-string> { RESTRICT | CASCADE }
  134.  
  135. drop-index-statement:
  136.     DROP INDEX <table-name-string>.<index-name-string>
  137.  
  138. expression:
  139.     {    <expression> OR <expression>
  140.       | <expression> AND <expression>
  141.       | NOT <expression>
  142.       | <expression> { = | <> | < | <= | > | >= } <expression>
  143.       | <expression> [ NOT ] LIKE <expression> [ ESCAPE <expression> ]
  144.       | <expression> { + | - } <expression>
  145.       | <expression> { * | / } <expression>
  146.       | -<expression>
  147.       | ( <expression> )
  148.       | <function>
  149.       | { <number> | <string-literal> | TRUE | FALSE }
  150.       | DATE <date-string> 
  151.       | TIME <time-string> 
  152.       | TIMESTAMP <timestamp-string>
  153.       | <column-reference>
  154.     }
  155.  
  156. column-reference:
  157.     {    <column-name-string>
  158.       | <range-variable-string>.<column-name-string>
  159.       | *
  160.       | <range-variable-string>.*
  161.     }
  162.       
  163. function:
  164.     {    BIT_LENGTH ( <expression> )
  165.       | OCTET_LENGTH ( <expression> )
  166.       | CHAR_LENGTH ( <expression> )
  167.       | CURRENT_DATE
  168.       | CURRENT_TIME
  169.       | CURRENT_DATETIME
  170.       | LOWER ( <expression> )
  171.       | UPPER ( <expression> )
  172.       | POSITION ( <expression> IN <expression> )
  173.       | SUBSTRING ( <expression> FROM <expression> [ FOR <expression> ] )
  174.       | TRIM ( [ LEADING | TRAILING | BOTH ]
  175.                [ <expression> ] [ FROM ] <expression> )
  176.       | AVG ( [ DISTINCT | ALL ] expression> )
  177.       | MAX ( [ DISTINCT | ALL ] <expression> )
  178.       | MIN ( [ DISTINCT | ALL ] <expression> )
  179.       | SUM ( [ DISTINCT | ALL ] <expression> )
  180.       | COUNT ( [ DISTINCT | ALL ] <expression> )
  181.       | COUNT (*)
  182.       | LAST_ROWID ( 'table-name-string' )
  183.     }
  184.  
  185. Note that in ALTER TABLE and DROP TABLE, there is no difference between RESTRICT and CASCADE.  In the TRIM function, the FROM keyword is required if either of the two preceeding parts (LEADING/TRAILING/BOTH and/or the pad character) are specified.
  186.  
  187.  
  188. Result Codes
  189.  
  190. The new REAL database returns an error code as well as an error message.  In the error message, the first five characters are the SQL standard error code, as follows:
  191.  
  192.  00000 -- Success
  193.  02000 -- No records found.
  194.  01004 -- Warning: String truncated on right.
  195.  01S09 -- Warning: Invalid SQL keyword.
  196.  07000 -- Error: Dynamic SQL error.
  197.  42S01 -- Error: Table already exists.
  198.  42S02 -- Error: Table not found.
  199.  42S11 -- Error: Index already exists.
  200.  42S12 -- Error: Index not found.
  201.  42S21 -- Error: Column already exists.
  202.  42S22 -- Error: Column not found.
  203.  21S01 -- Error: Insert value list does not match column list.
  204.  22000 -- Error: Data exception.
  205.  22023 -- Error: Invalid parameter.
  206.  25000 -- Error: Invalid transaction state.
  207.  0A000 -- Error: Unsupported SQL feature.
  208.  72000 -- Error: Internal SQL processor error.
  209.  
  210.  
  211. SQL Examples
  212.  
  213. The following examples assume a database with two tables: Movies (containing Title, Director, and Year), and Actors (containing Name and Movie, where Movie holds the _rowid value of the corresponding row in the Movies table).  For each example, we present a plain English description, the SQL syntax, the result code, and the values returned by the query (where applicable, and assuming a handful of pre-existing data).
  214.  
  215. EXAMPLE: Prove that SQL can do math.
  216. SQL: select 2+3+2*3
  217. RESULT: 00000
  218. 11
  219.  
  220. EXAMPLE: List all fields for all movies in the database.
  221. SQL: select * from Movies
  222. RESULT: 00000
  223. 1,Star Wars,George Lucas,1977
  224. 2,Raiders of the Lost Ark,George Lucas,1981
  225. 3,American Graffiti,George Lucas,1973
  226. 4,Apollo 13,Ron Howard,1995
  227. 5,Cast Away,Robert Zemeckis,2000
  228.  
  229. EXAMPLE: List distinct actors (i.e., don't list any actor more than once).
  230. SQL: select distinct Name from Actors
  231. RESULT: 00000
  232. Harrison Ford
  233. Mark Hamill
  234. Carrie Fisher
  235. Ron Howard
  236. Richard Dreyfuss
  237. Tom Hanks
  238.  
  239. EXAMPLE: Find all actors who were in Star Wars.
  240. SQL: select a.name from Actors A, Movies M where a.movie=m._rowid and m.title='Star Wars'
  241. RESULT: 00000
  242. Harrison Ford
  243. Mark Hamill
  244. Carrie Fisher
  245.  
  246. EXAMPLE: Find actors, with movie title and year, for all movies directed by George Lucas.
  247. SQL: select A.Name,M.Title,M.Year from Actors A, Movies M where A.movie=M._rowid and M.Director='George Lucas'
  248. RESULT: 00000
  249. Ron Howard,American Graffiti,1973
  250. Richard Dreyfuss,American Graffiti,1973
  251. Harrison Ford,Raiders of the Lost Ark,1981
  252. Harrison Ford,Star Wars,1977
  253. Mark Hamill,Star Wars,1977
  254. Carrie Fisher,Star Wars,1977
  255.  
  256. EXAMPLE: See if Joe has been in any movies.
  257. SQL: select Name from Actors where Name='Joe Strout'
  258. RESULT: 02000
  259.  
  260. EXAMPLE: Find all actors who have done a movie with Harrison Ford.
  261. SQL: select A.name from Actors A, Actors B where A.movie = B.movie and B.name='Harrison Ford' and A.name<>'Harrison Ford'
  262. RESULT: 00000
  263. Mark Hamill
  264. Carrie Fisher
  265.  
  266. EXAMPLE: Find anyone who is both an actor and a director.
  267. SQL: select A.Name from Actors A, Movies M where A.Name = M.Director
  268. RESULT: 00000
  269. Ron Howard
  270.  
  271. EXAMPLE: Add a new record to the Movies table.
  272. SQL: insert into Movies (title, director, year) values ('Joe''s Home Movie', 'Joe Strout', 2003)
  273. RESULT: 00000
  274.  
  275. EXAMPLE: Verify that the record was added correctly.
  276. SQL: select director, title from Movies where title = 'Joe''s Home Movie'
  277. RESULT: 00000
  278. Joe Strout,Joe's Home Movie
  279.  
  280. EXAMPLE: Delete the record we just added.
  281. SQL: delete from Movies where director='Joe Strout'
  282. RESULT: 00000
  283.  
  284. EXAMPLE: Attempt a table alteration while there's a transaction in progress.
  285. SQL: alter table Movies add column Rating integer
  286. RESULT: 25000
  287.  
  288. EXAMPLE: Find any movies with the word "War" in the title (ignoring case).
  289. SQL: select title, year from Movies where Upper(title) like '%WAR%'
  290. RESULT: 00000
  291. Star Wars,1977
  292.  
  293. EXAMPLE: Find the director of any move not made in the 1900s, and count the characters in his name.
  294. SQL: select director, char_length(director) from Movies where year not like '19__'
  295. RESULT: 00000
  296. Robert Zemeckis,15
  297.  
  298. EXAMPLE: Find out when each director made his first and last movie (in our database).
  299. SQL: select Director, Min(Year), Max(Year) from Movies group by Director
  300. RESULT: 00000
  301. George Lucas,1973,1981
  302. Robert Zemeckis,2000,2000
  303. Ron Howard,1995,1995
  304.  
  305. EXAMPLE: Find out how many actors we have for each movie (by title).
  306. SQL: select A.Title, Count(B.Name) from Movies A, Actors B where B.Movie = A._rowID group by B.Movie
  307. RESULT: 00000
  308. Star Wars,3
  309. Raiders of the Lost Ark,1
  310. American Graffiti,2
  311. Apollo 13,1
  312. Cast Away,1
  313.  
  314. EXAMPLE: Find how many movies each actor has done, but display only those who have done more than one.
  315. SQL: select Name, Count(*) from Actors group by Name having Count(*) > 1
  316.  
  317.  
  318. Known Limitations
  319.  
  320. No table can be greater than 2 GB total size.  When storing files in a virtual volume, the entire database must also be under 2 GB.  Row IDs increment automatically and are never reused; so you can't insert more than 2^32 (about 4 billion) records into any table. [We intend to remove that limitation.]
  321.  
  322.  
  323. ----
  324. Document Modified: 05 Aug 2003
  325.